package com.bob.shirodemo.service;

import com.bob.shirodemo.entity.ResourceEntity;
import com.bob.shirodemo.entity.RoleEntity;
import com.bob.shirodemo.entity.UserEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

@Service
public class UserService {

    /**
     * 根据用户名称获得角色实体
     * @param username
     * @return
     */
    public Set<RoleEntity> getRolesByUsername(String username) {
        //模拟数据，这里的数据来源可以是数据库或是其他的地方
        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setName("admin");
        //添加角色拥有的资源
        roleEntity.setResources(new ArrayList<ResourceEntity>(){{
            ResourceEntity resource = new ResourceEntity();
            resource.setName("测试");
            resource.setUri("/test");
            add(resource);
        }});
        return new HashSet<RoleEntity>() {
            {
               add(roleEntity);
            }
        };
    }

    /**
     * 根据token获得用户
     * @param token
     * @return
     */
    public UserEntity getUserByToken(String token) {
        //模拟根据token获得数据
        UserEntity user = new UserEntity();
        user.setUsername("管理员");
        user.setPassword("123");
        user.setRoles(new ArrayList<RoleEntity>(){{
            RoleEntity roleEntity = new RoleEntity();
            roleEntity.setName("admin");
            add(roleEntity);
        }});
        return user;
    }
}
